import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author YuanYuan
 * @Date 2022/11/26
 * @Time 11:17
 */
@WebServlet("/blog")
public class BlogServlet extends HttpServlet {
    private ObjectMapper objectMapper = new ObjectMapper();

    //发送回应-显示博客列表
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //按照约定的接口格式返回数据
        resp.setContentType("application/json;charset=utf8");

        try {
            List<Blog> blogs = BlogDAO.selectAll();
            resp.getWriter().write(objectMapper.writeValueAsString(blogs));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
